/*
 * Afin de recuperer un image comme composant 
 * la table de l'arriere plan !
 */
package ubo.master1.jeupoker.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;

public class JPanelImageBg extends JComponent {

    private BufferedImage bufferedImage;

    JPanelImageBg(String fileName) {
        this.bufferedImage = this.toBufferedImage(Toolkit.getDefaultToolkit().getImage(fileName));

    }

    public void paintComponent(Graphics g) {
        g.setColor(this.getBackground());
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(bufferedImage, (getWidth() - bufferedImage.getWidth()) / 2, (getHeight() - bufferedImage.getHeight()) / 2, null);

    }

    private BufferedImage toBufferedImage(Image image) {
        image = new ImageIcon(image).getImage();

        BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics g = bufferedImage.createGraphics();

        g.setColor(Color.white);
        g.fillRect(0, 0, image.getWidth(null),
                image.getHeight(null));
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return bufferedImage;
    }
}
